<template>
  <el-dialog :title="!dataForm.addrId ? '新增' : '修改'"
             :close-on-click-modal="false"
             :visible.sync="visible">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="100px">

      <el-form-item label="状态">
        <el-col :span="8">
          <el-form-item prop="province">
            <el-select v-model="dataForm.provinceId"
                       placeholder="请选择"
                       @change="selectProvince">
              <el-option v-for="province in provinceList1"
                         :key="province"
                         :label="province"
                         :value="province"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="备注信息"
                    prop="receiver">
        <el-input v-model="dataForm.receiver"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      roleList: [],
      provinceList: [],
      provinceList1: ['已通过','未通过','待处理'],
      cityList: [],
      areaList: [],
      dataForm: {
        addrId: null,
        userId: null,
        receiver: null,
        provinceId: null,
        province: null,
        city: null,
        cityId: null,
        area: null,
        areaId: null,
        postCode: null,
        addr: null,
        mobile: null,
        status: 1,
        commonAddr: 0,
        rmb:null,
        carryId:null
      },
      dataRule: {
      }
    }
  },
  methods: {
    init (addrId,rmb,carryId) {

      this.dataForm.addrId = addrId || 0
      this.dataForm.rmb = rmb || 0
      this.dataForm.carryId = carryId || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.cityList = []
        this.areaList = []
        this.dataForm.provinceId = null
        this.dataForm.cityId = null
        this.dataForm.areaId = null
        if (this.dataForm.addrId) {
          console.log(addrId,this.dataForm.receiver,this.dataForm)
          // this.$http({
          //   url: this.$http.adornUrl(`/admin/carry/editCarry`),
          //   method: 'get',
          //   params: this.$http.adornParams()
          // }).then(({ data }) => {
          //   this.dataForm = data
          //   this.dataForm.addr = data.addr
          //   this.dataForm.addrName = data.addrName
          //   this.dataForm.areaId = data.areaId
          //   this.dataForm.cityId = data.cityId
          //   this.dataForm.provinceId = data.provinceId
          //   this.listAreaByParentId(data.provinceId).then(({ data }) => {
          //     this.cityList = data
          //   })
          //   this.listAreaByParentId(data.cityId).then(({ data }) => {
          //     this.areaList = data
          //   })
          // })
        }
      })
      this.listAreaByParentId().then(({ data }) => {
        this.provinceList = data
      })
    },
    listAreaByParentId (pid) {
      if (!pid) pid = 0
      return this.$http({
        url: this.$http.adornUrl(`/admin/carry/queryCarry`),
        method: 'get',
        params: this.$http.adornParams({ pid })
      })
    },
    // 选择省
    selectProvince (val) {
      this.dataForm.cityId = null
      this.dataForm.city = ''
      // 获取城市的select
      this.listAreaByParentId(val).then(({ data }) => {
        this.cityList = data
      })
    },
    // 选择市
    selectCity (val) {
      this.dataForm.areaId = null
      this.dataForm.area = ''
      // 获取区的select
      this.listAreaByParentId(val).then(({ data }) => {
        this.areaList = data
      })
    },

    // 表单提交
    dataFormSubmit () {
      let that = this
      let a
      console.log(that.dataForm)
      if (this.dataForm.provinceId == "已通过") {
        a = 1
      }
      if (this.dataForm.provinceId == "未通过") {
        a = 2
      }
      if (this.dataForm.provinceId == "待处理") {
        a = 3
      }
      if (this.dataForm.provinceId == null){
        alert("必须选择状态")
      }else{
        this.$http({
          url: this.$http.adornUrl(`/admin/carry/editCarry`),
          method: 'post',
          data: this.$http.adornData({
            "userId": this.dataForm.addrId,
            "remarks": this.dataForm.receiver,
            "state": a,
            "rmb": this.dataForm.rmb,
            "carryId": this.dataForm.carryId,
          })
        }).then(({ data }) => {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList", this.page);
            }
          });
        });
      }

    }
  }
}
</script>
